//
//  HERetrunLogCodeCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/16.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HERetrunLogCodeCell.h"

@interface HERetrunLogCodeCell ()<QMUITextFieldDelegate>

@property (nonatomic, strong) QMUITextField *searchTF;

@end

@implementation HERetrunLogCodeCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
 
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    UIView *warp = [UIView new];
    warp.backgroundColor = CSplitLineColor;
    [self.contentView addSubview: warp];
    
    [warp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(15);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
        make.bottom.equalTo(self.contentView).mas_offset(-15);
    }];
    
    UILabel *leftLb = [UILabel new];
    leftLb.text = @"快递编号";
    leftLb.font = DYFont(16);
    [self.contentView addSubview: leftLb];
    [leftLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(warp).mas_offset(15);
        make.centerY.equalTo(warp);
        make.width.mas_offset([leftLb.text widthForFont:leftLb.font]);
    }];
    
    [leftLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    // 输入框
    QMUITextField *searchTF = [[QMUITextField alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth-60, 34)];
    _searchTF = searchTF;

    searchTF.backgroundColor = UIColor.clearColor;
    searchTF.font = DYFont(16);
    
    searchTF.placeholder = @"快递编号";
    searchTF.placeholderColor = [UIColor colorWithHexString:@"#AAAAAA"];
    searchTF.delegate = self;
    
    UIView *rightWarp = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 49, 18)];
    UIImageView *leftIcon = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"saoyisao")];
    leftIcon.frame = CGRectMake(20, 0, 18, 18);
    [rightWarp addSubview:leftIcon];
    searchTF.rightViewMode = UITextFieldViewModeAlways;
    searchTF.rightView = rightWarp;
    [warp addSubview:searchTF];
    
//    leftIcon.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(qrcodeClick)];
    [rightWarp addGestureRecognizer:tap];
    
    [searchTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(warp).mas_offset(10);
        make.left.equalTo(leftLb.mas_right).mas_offset(10);
        make.right.equalTo(warp.mas_right).mas_offset(-10);
        make.height.mas_offset(20);
        make.bottom.equalTo(warp).mas_offset(-10);
    }];
}

- (void)refreshContent:(GDFiterCellModel *)fiterModel formModel:(GDFormModel *)formModel indexPath:(NSIndexPath *)indexPath {
    self.delegate = formModel.proxyVC;
    self.searchTF.text = formModel.as_logCode;
}

- (void)qrcodeClick {
    if ([_delegate respondsToSelector:@selector(qrcodeWithCell:)]) {
        [_delegate qrcodeWithCell:self];
    }
}

#pragma mark -
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
    self.formModel.as_logCode = text;
    return YES;
}

@end
